﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dasBlog.Services;
using System.ServiceModel;
using dasBlog.Storage;
using System.ServiceBus;
using System.ServiceModel.Description;

namespace dasBlog.CloudStorage
{
    public class CloudStorageRuntime : Host
    {
        ServiceHost storageBusHost;
        ServiceHost textStorageHost;

        public CloudStorageRuntime()
        {

        }

        public void Start(string username, string password)
        {
            var address = new Uri(String.Format("sb://{0}/services/{1}/storagebus/", RelayBinding.DefaultRelayHostName, username));
            var usernameTokenProvider = new UsernameTokenProvider(username, password);
            var relayBinding = new RelayBinding();
            relayBinding.ConnectionMode = RelayConnectionMode.HybridDuplexSession;
            relayBinding.NameSettings.AllowBrowsing=true;
            relayBinding.Security.Enabled = false;
            relayBinding.Chunking.Enabled = true;
            relayBinding.Chunking.ChunkSize = 1024 * 64;
                        
            
            storageBusHost = new ServiceHost(StorageBus.Current, address);
            storageBusHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
            var storageBusEndpoint = storageBusHost.AddServiceEndpoint(
                                typeof(IStorageBus),
                                relayBinding, "");
            storageBusEndpoint.Behaviors.Add(usernameTokenProvider);
            var storageBusMetadataEndpoint = storageBusHost.AddServiceEndpoint(
                                typeof(IMetadataExchange),
                                relayBinding, "mex");
            storageBusMetadataEndpoint.Behaviors.Add(usernameTokenProvider);
            
            var textaddress = new Uri(String.Format("sb://{0}/services/{1}/textstorage/", RelayBinding.DefaultRelayHostName, username));
            
            IStorageNode textStorageNode = StorageNodeFactory.CreateStorageNode<dasBlog.Storage.SqlServer.TextEntryStorageProvider>();
            
            textStorageHost = new ServiceHost(textStorageNode, textaddress);
            textStorageHost.Description.Behaviors.Add(new ServiceMetadataBehavior());
            var textNodeEndpoint = textStorageHost.AddServiceEndpoint(
                                typeof(IStorageNode),
                                relayBinding, "");
            textNodeEndpoint.Behaviors.Add(usernameTokenProvider);
            var textNodeMetadataEndpoint = textStorageHost.AddServiceEndpoint(
                                typeof(IMetadataExchange),
                                relayBinding, "mex");
            textNodeMetadataEndpoint.Behaviors.Add(usernameTokenProvider);


            storageBusHost.Open();
            textStorageHost.Open();
            

        }

        public void Stop()
        {
            storageBusHost.Close();
            textStorageHost.Close();
        }

    }
}
